package com.bw.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.bw.entity.PageResult;
import com.bw.entity.Result;
import com.bw.pojo.comment.ItemProductPages;
import com.bw.pojo.comment.ProductReply;
import com.bw.pojo.comment.ProductReplyPages;
import com.bw.pojo.order.OrderItem;
import com.bw.service.comment.ProductReplyService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/productReply")
public class ProductReplyController {
    @Reference
    private ProductReplyService productReplyService;

    //查询全部信息
    @GetMapping("/findAll")
    public List<ProductReply>findAll(){
        return productReplyService.findAll();
    }

    //查询全部评论（用于管理员）
    @GetMapping("/findPage")
    public PageResult<ProductReply> findPage(int page, int size){
        return  productReplyService.findPage(page,size);
    }

//    //查询该sku下的评论（用于展示某商品的评论）
//    @GetMapping("/findPageBySkuId")
//    public ProductReplyPages findPageBySkuId(int page, int size, String skuId){
//        return productReplyService.findPageBySkuId(page,size,skuId);
//    }
    
    //分页+查询，可用于当前用户查询评论，或者管理员根据信息查询评论
    @PostMapping("/findPageSearch")
    public PageResult<ProductReply> findPageSearch(@RequestBody Map<String, Object> searchMap, int page, int size) {
        return productReplyService.findPageSearch(searchMap,page,size);
    }


    @GetMapping("findItemByUsername")
    public  PageResult<ItemProductPages> findItemByUsername(int page,int size){
//        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        return  productReplyService.findItemPage(page,size,"lijialong");
    }

    @GetMapping("/findByOrderId")
    public ProductReply findByOrderId(String orderId){
        return productReplyService.findByOrderId(orderId);
    }

    @GetMapping("/findById")
    public ProductReply findById(String id){
        ProductReply byId = productReplyService.findById(id);
        return byId;
    }
    //删除评论
    @GetMapping("/dele")
    public void dele(String id){
        productReplyService.delete(id);
    }
    //恢复删除的评论
    @GetMapping("restore")
    public void restore(String id){
        productReplyService.restore(id);
    }

    //添加第一次的评论
    @GetMapping("/add")
    public void add(String orderId,String skuId){productReplyService.add(orderId,skuId);}

    @PostMapping("/add1")
    public void add1(@RequestBody ProductReply productReply){
        if (productReply.getIsAnonymous() == "true") productReply.setIsAnonymous("0");
        else productReply.setIsAnonymous("1");
        System.out.println(productReply.getReplyContent());
        System.out.println(productReply.getIsAnonymous());
        System.out.println(productReply.getSkuId());
        System.out.println(productReply.getOrderId());
        productReplyService.add1(productReply);
//        productReplyService.add1(searchMap);
    }

    //追评
    @PostMapping("/follow")
    public void follow(@RequestBody ProductReply productReply){
        System.out.println(productReply.getFollowContent());
        productReplyService.follow(productReply.getId(),productReply.getFollowContent(),productReply.getFollowImage());
    }

    //超时自动好评
    @GetMapping("/timeOut")
    public void TimeOut(){
        productReplyService.TimeOut();
    }

    /**
     * 删除追评
     */
    @GetMapping("/deleteFollow")
    public Result deleteFollow(String id){
        productReplyService.deleteFollow(id);
        return new Result();
    }

    //
    @GetMapping("/browsesAdd")
    public Result browseAdd(String id){
        productReplyService.browsesAdd(id);
        return new Result();
    }


}
